package game.data;

import game.data.FightAnalysisData.FightAnalysisType;

/**
 * 游戏战斗分析日志数据
 */
class GameFightAnalysisLogData {
	public function new() {}

	/**
	 * 玩家数据
	 */
	public var playerData:Map<String, MultiFightAnalysisData> = [];

	/**
	 * 追加玩家数据
	 * @param rolePlayerId 
	 * @param id 
	 * @param data 
	 */
	public function pushData(rolePlayerId:Int, id:String, data:FightAnalysisData):Void {
		var key = rolePlayerId + "_" + id;
		if (playerData.exists(key)) {
			playerData.get(key).data.push(data);
		} else {
			playerData.set(key, new MultiFightAnalysisData([data]));
		}
	}

	/**
	 * 获得玩家数据
	 * @param rolePlayerId 
	 * @param id 
	 * @return Array<FightAnalysisData>
	 */
	public function getData(rolePlayerId:Int, id:String):MultiFightAnalysisData {
		var key = rolePlayerId + "_" + id;
		if (playerData.exists(key)) {
			return playerData.get(key);
		}
		return new MultiFightAnalysisData();
	}
}

/**
 * 复合战斗数据记录
 */
class MultiFightAnalysisData {
	public var data:Array<FightAnalysisData>;

	public function new(?list:Array<FightAnalysisData>) {
		data = list;
		if (data == null) {
			data = [];
		}
	}

	/**
	 * 获得最大值
	 * @param type 
	 * @return Float
	 */
	public function getMaxValue(type:FightAnalysisType):Float {
		if (data.length == 0) {
			return 0;
		}
		data.sort((a, b) -> {
			var v1 = a.getEventValue(type);
			var v2 = b.getEventValue(type);
			return v1 > v2 ? -1 : 1;
		});
		return data[0].getEventValue(type);
	}

	/**
	 * 获得最小值
	 * @param type 
	 * @return Float
	 */
	public function getMinValue(type:FightAnalysisType):Float {
		if (data.length == 0) {
			return 0;
		}
		data.sort((a, b) -> {
			var v1 = a.getEventValue(type);
			var v2 = b.getEventValue(type);
			return v1 > v2 ? -1 : 1;
		});
		return data[data.length - 1].getEventValue(type);
	}

	/**
	 * 获得平均值
	 */
	public function getAvgValue(type:FightAnalysisType):Float {
		if (data.length == 0) {
			return 0;
		}
		var sum = 0.;
		for (i in 0...data.length) {
			sum += data[i].getEventValue(type);
		}
		return sum / data.length;
	}

	public function getAvgScrore():Float {
		if (data.length == 0) {
			return 0;
		}
		var sum = 0.;
		for (i in 0...data.length) {
			sum += data[i].getScore();
		}
		return sum / data.length;
	}
}
